﻿P.Module.define("Grid", [],
    function()
    {
        var e = "hasError";
        return P.Module.extend({
            events: {
                "click .gridFooter .gridError .Button": "onTryAgainClick"
            },
            initialize: function(e)
            {
                e.options.show_grid_footer && (this.childListen(P.CONST.EVENT_ERROR, this.onError), this.childListen(P.CONST.EVENT_ERROR_RESOLVED, this.onErrorResolved), this.childListen(P.CONST.EVENT_BUTTON_CLICK, this.onButtonClick), this.childListen(P.CONST.EVENT_ITEMS_REQUEST_START, this.onItemsRequestStart), this.childListen(P.CONST.EVENT_ITEMS_REQUEST_SUCCESS, this.onItemsRequestSuccess), this.childListen(P.CONST.EVENT_ITEMS_END, this.onItemsEnd));
            },
            onContentReady: function()
            {
                this.items = this.getChildByModuleName("GridItems"),
                this.footer = this.getChildByModuleName("GridFooter"),
                this.footer && this.hasItems() && this.footer.show();
            },
            hasItems: function()
            {
                return this.items.hasItems();
            },
            hasItemsOfType: function(e)
            {
                return !!this.items.getChildrenByModuleName(e).length;
            },
            hasMoreItems: function()
            {
                return this.items.hasMoreItems();
            },
            requestMoreItems: function()
            {
                this.items.requestMoreItems();
            },
            emptyItems: function()
            {
                this.items.empty();
            },
            renderItems: function(e, t)
            {
                this.items.render(e, t);
            },
            setPageSize: function(e)
            {
                this.items.setPageSize(e);
            },
            getItemsResource: function()
            {
                return this.items.resource;
            },
            setItemsResource: function(e)
            {
                this.setResource(e),
                this.items && this.items.setResource(e);
            },
            onError: function()
            {
                this.hasError = !0,
                this.$el.addClass(e),
                this.footer && (this.footer.showError(), this.footer.hideGridLoading());
            },
            onErrorResolved: function()
            {
                this.hasError && (this.hasError = !1, this.$el.removeClass(e));
            },
            onButtonClick: function()
            {
                return this.requestMoreItems(),
                !1;
            },
            onItemsRequestStart: function()
            {
                this.footer && this.footer.showGridLoading();
            },
            onItemsRequestSuccess: function()
            {
                this.footer && this.footer.hideError();
            },
            onItemsEnd: function()
            {
                this.footer && this.footer.hideGridLoading();
            },
            prependChildItem: function(e)
            {
                this.getGridItems().prependChildItem(e);
            },
            prependNewItems: function(e, t)
            {
                return this.getGridItems().prependNewItems(e, t);
            },
            getGridItems: function()
            {
                return this.items;
            },
            getFirstGridItem: function(e)
            {
                return this.getGridItems().getChildByModuleName(e);
            }
        });
    });